<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI Web Develop
 *
 * @package     HITI Web Dev     
 * @category    Controllers
 * @see 		Orders
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Orders extends CI_Controller {
	var $header;
	var $footer;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_common_model', 'hiti', true);
		$this->hiti->hiti_init();		
		$this->header = $this->hiti->header_templ();
		$this->footer = $this->hiti->footer_templ();
		$this->load->model('hiti_carts_model', 'cart', true);
	}

	public function index()
	{
		redirect('index.html');
	}

	public function view($ma)
	{
		if (!$ma)
		{
			redirect('index.html');
		}
		$info = $this->cart->info($ma);
		if ($this->input->get('paypal', true) == 'success')
		{
			$carts = $this->cart->details($ma);
			$items_str = '';
			$i = 0;
			$subtotal = 0;
			foreach ($carts as $cart)
			{
				$itemtotal = vnd2usd($cart['gia']) * $cart['soluong'];
				$subtotal += $itemtotal;
				$items_str .= '&L_NAME'.$i.'='.urlencode($cart['name']).'&L_NUMBER'.$i.'='.($i+1).'&L_DESC'.$i.'='.urlencode($cart['code']).'&L_AMT'.$i.'='.urlencode(vnd2usd($cart['gia'])).'&L_QTY'.$i.'='.(int)$cart['soluong'].'';
				$i++;
			}
			$vat = 0;
			if ($info['vat']) $vat = $subtotal * 0.1;
	        $ship = vnd2usd($info['phi_giaohang']);
	        $total = $subtotal + $vat + $ship;
			$return = base_url().'don-hang/'.$ma.'?paypal=success';
			$cancel = base_url().'don-hang/'.$ma.'?paypal=cancel';
			$logo = base_url().'images/logo.png';
			$payer = $this->input->get('PayerID');	
			$token = $this->input->get('token');
			$httpResponseArray = paypal_checkout($payer, $token, $ma, $subtotal, $vat, $ship, $total, $items_str, $return, $cancel, $logo);
			if (is_array($httpResponseArray) && count($httpResponseArray))
			{				
				$this->cart->update_checkout($ma, array('status' => 3));
			}
			else
			{
				$this->cart->update_checkout($ma, array('status' => 4));
			}
			redirect(base_url().'don-hang/'.$ma);
		}
		else if ($this->input->get('paypal', true) == 'cancel')
		{
			$this->cart->update_checkout($ma, array('status' => 4));
			redirect(base_url().'don-hang/'.$ma);
		}
		else if ($this->input->get('nganluong', true) == 'success')
		{
			if(nganluong_verifypaymenturl())
			{
				$this->cart->update_checkout($ma, array('status' => 3));
			}
			else
			{
				$this->cart->update_checkout($ma, array('status' => 4));
			}
			redirect(base_url().'don-hang/'.$ma);
		}
		else if ($this->input->get('baokim', true) == 'success')
		{
			if(baokim_verifyresponseurl())
			{
				$this->cart->update_checkout($ma, array('status' => 4));	
			}
			else
			{
				$this->cart->update_checkout($ma, array('status' => 4));
			}
			redirect(base_url().'don-hang/'.$ma);
		}
		else if ($this->input->get('baokim', true) == 'cancel')
		{
			$this->cart->update_checkout($ma, array('status' => 4));
			redirect(base_url().'don-hang/'.$ma);
		}
		else if ($this->input->get('sohapay', true) == 'success')
		{
			if(sohapay_verifyreturnurl())
			{
				$this->cart->update_checkout($ma, array('status' => 3));
			}
			else
			{
				$this->cart->update_checkout($ma, array('status' => 4));
			}
			redirect(base_url().'don-hang/'.$ma);
		}
		$home = array(
			'info' => $info,
			'carts' => $this->cart->details($ma)
		);
		$GLOBALS['cfg']['site_title'] = $GLOBALS['lng']['check_order'];
		/*
		* Hien thi
		*/
		$this->load->view('header', $this->header);
		$this->load->view('orders', $home);
		$this->load->view('footer', $this->footer);
	}

	public function printer($ma)
	{
		if (!$ma)
		{
			redirect('index.html');
		}
		$info = $this->cart->info($ma);
		$home = array(
			'info' => $info,
			'carts' => $this->cart->details($ma)
		);
		/*
		* Hien thi
		*/
		$this->load->view('home/order_printer', $home);
	}
}

/* End of file orders.php */
/* Location: ./application/controllers/orders.php */